package com.itheima.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BusinessException;
import com.itheima.reggie.dao.CategoryMapper;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    /*
    * 根据id删除,删除前进行判断
    * */
    @Override
    public void remove(Long id) {
        //1、检查当前分类下是否有对应的菜品  --> 然后抛出一个自定义业务异常
        LambdaQueryWrapper<Dish> dqw = new LambdaQueryWrapper<>();
        dqw.eq(Dish::getCategoryId,id);
        int count = dishService.count(dqw);
        if (count>0){
            //该分类下有菜品，不能删除-->抛出一个自定义业务异常
            throw new BusinessException("当前该分类下有菜品，不能删除");
        }

        //2、检查当前分类下是否有对应的套餐  --> 然后抛出一个自定义业务异常
        LambdaQueryWrapper<Setmeal> sqw = new LambdaQueryWrapper<>();
        sqw.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(sqw);
        if (count1>0){
            //该分类下有套餐，不能删除-->抛出一个自定义业务异常
            throw new BusinessException("当前该分类下有套餐，不能删除");
        }

        //3、可以删除,则调用mapper直接删除 categoryMapper.deleteById(id);
        super.removeById(id);
    }

}
